// pages/buyticket/buyticket.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,

    hidden: true,
    // showView: true,
    flag1: true,
    title: [
      "你好我是好人你好我是好人你好我是好人你好我是好人",
      "你好我是好人",
      "你好我是好人"
    ],
    beijingheight: 0,
    top:0,
    left:0,
    dataInfo: [],
    userInfo: {
      code: ''
    },
    line: {

    },
    x:0,
    y:0,
    xyyidong:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

    var showView = that.data.showView
    showView: (options.showView == "true" ? true : false)
    var height = that.data.height
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          height: res.windowHeight,
          beijingheight: res.windowHeight,
          x:res.windowHeight,
          y:res.windowWidth
        })
      },
    })

    wx.request({
      url: app.globalData.url+'/actualRoute/actuallist',
      success: function(dd) {
        console.log(dd.data)
        var arr = []
        for (var i = 0; i < dd.data.length; i++) {
          dd.data[i].start = dd.data[i].sites[0]
          dd.data[i].stop = dd.data[i].sites[dd.data[i].sites.length - 1]
          dd.data[i].sites.splice(0, 1)
          dd.data[i].sites.splice(dd.data[i].sites.length - 1, 1)
          dd.data.flag = false
          // console.log(dd.data[i])
        }

        that.setData({
          luxian: dd.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  expandDetail: function(e) {
    var idx = e.currentTarget.dataset.idx, // 获取当前下标
      key = "luxian[" + idx + "].flag",
      val = this.data.luxian[idx].flag;
    this.setData({
      [key]: !val
    });
  },
  buy: function(e) {
    var index = e.currentTarget.dataset;

    wx.navigateTo({
      url: '../../pages/pay/pay?id=' + index.id + '&start=' + index.start + '&stop=' + index.stop + '&starttime=' + index.starttime + '&stoptime=' + index.stoptime + '&stopsiteid=' + index.stopsiteid + '&startsiteid=' + index.startsiteid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  nayhmd(){
    wx.navigateTo({
      url: '../../pages/salebuy/salebuy',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.login({
      success(res) {
        // that.data.userInfo.code = res.code;
        console.log(res.code);
        that.setData({
          userInfo: {
            code: res.code
          }
        })
      }
    })
    var that = this
    var flag1 = that.data.flag1


    var weixinnickname1 = wx.getStorageSync("weixinnickname")
    var headimgurl1 = wx.getStorageSync("headimgurl")
    if (weixinnickname1 == "" || headimgurl1 == "") {

      that.setData({
        flag1: false
      })
      // that.access()

    } else {

      that.setData({

        flag1: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  ditu: function(e) {
    var index = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../../pages/map/map?id=' + index.idx + '&lineid=' + index.lineid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  access(res) {
    console.log(res)
    var userInfo = res.detail,
      that = this;
    var flag1 = that.data.flag1
    wx.request({
      url: app.globalData.url + "/wechat/decodeUserInfo", //仅为示例，并非真实的接口地址
      data: {
        encryptedData: userInfo.encryptedData,
        iv: userInfo.iv,
        code: that.data.userInfo.code,

      },
      success(res) {
        console.log(res)
        that.data.userInfo = res.data.userInfo;
        var openid = res.data.userInfo.openid
        wx.setStorageSync("openid", openid)
        var weixinnickname = res.data.userInfo.weixinnickname
        var headimgurl = res.data.userInfo.headimgurl
        wx.setStorageSync("weixinnickname", weixinnickname)
        wx.setStorageSync("headimgurl", headimgurl)
        wx.setStorageSync("userid", res.data.userInfo.id)
        app.globalData.userInfo = res.data.userInfo
        that.shop();
        that.setData({
          headimgurl: headimgurl,
          weixinnickname: weixinnickname,
          flag1: true
        })
      }
    })
  },
  shop() {
    var that = this;
    var stop = that.data.stopsiteid
    var start = that.data.startsiteid
    var userid = that.data.userInfo.id
    var openid = that.data.userInfo.openid
    wx.setStorageSync("userid", userid)
    wx.setStorageSync("openid", openid)
    wx.setStorageSync("lineid", that.data.line.id)
    wx.setStorageSync("start", start)
    wx.setStorageSync("stop", stop)

  },
})